package com.tao.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tao.common.utils.PageUtils;
import com.tao.common.utils.Query;

import com.tao.gulimall.product.dao.ProductAttrValueDao;
import com.tao.gulimall.product.entity.ProductAttrValueEntity;
import com.tao.gulimall.product.service.ProductAttrValueService;
import org.springframework.transaction.annotation.Transactional;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

	@Override
	public void saveProductAttr(List<ProductAttrValueEntity> attrValueEntities) {
		this.saveBatch(attrValueEntities);
	}

	@Override
	public List<ProductAttrValueEntity> baseAttrlistforspu(Long spuId) {
		return this.baseMapper.selectList(new LambdaQueryWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getId, spuId));


	}

	@Override
	@Transactional
	public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> list) {
		// 先全部删除
		this.remove(new LambdaQueryWrapper<ProductAttrValueEntity>().eq(ProductAttrValueEntity::getSpuId,spuId));
		// 然后再重新插入
		List<ProductAttrValueEntity> collect = list.stream().map(item -> {
			item.setSpuId(spuId);
			return item;
		}).collect(Collectors.toList());
		this.saveBatch(collect);
	}

}